package com.jerry.alibaba.transaction.service.impl;

import com.jerry.alibaba.transaction.entity.Order;
import com.jerry.alibaba.transaction.dao.OrderDao;
import com.jerry.alibaba.transaction.service.AccountFeignService;
import com.jerry.alibaba.transaction.service.OrderServcie;
import com.jerry.alibaba.transaction.service.StorageFeignService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderServcie {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageFeignService storageFeignService;
    @Resource
    private AccountFeignService accountFeignService;

    @Override
    @GlobalTransactional(name = "test_order_transaction", rollbackFor = Exception.class)
    public void create(Order order) {
        // 创建订单
        orderDao.create(order);
        // 扣减库存
        storageFeignService.decrease(order.getProductId(), order.getCount());
        // 扣减金额
        accountFeignService.decrease(order.getUserId(), order.getMoney());
        // 更新订单状态
        orderDao.update(order.getUserId());

    }

    @Override
    public Order getById(Long id) {
        return orderDao.getById(id);
    }

    @Override
    public List<Order> getList() {
        return orderDao.getList();
    }

}
